package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.event.listeners.MessageListener;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.util.Filter;
import com.rsbuddy.script.wrappers.Npc;

import webjoch.misc.Functions;
import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Room;
/**
 * 
 * @author Webjoch
 *
 */
public class Monolith extends Puzzle implements MessageListener{
	Room room;
	boolean solved;
	public final int[] NPC_MONOLITH = {10975, 10978};
	public final int[] SHADES = {10833, 10834, 10835, 10836};
	
	Npc monolith;
		
	@Override
	public void setRoom(Room room) {
		this.room = room;
	}
	
	@Override
	public Type getType() {
		return Type.MONOLITH;
	}

	public Npc getMonolith(){
		return room.getNpc(NPC_MONOLITH);
	}
	
	@Override
	public boolean isValid() {
		return getMonolith() != null && getMonolith().getAnimation() > 0;
	}
	
	@Override
	public int loop(){
		if (Widgets.getComponent(945, 2).isVisible()){
			Npc shade = Npcs.getNearest(new Filter<Npc>(){
				public boolean accept(Npc npc){
					return Functions.inArray(SHADES, npc.getId()) && room.getArea().contains(npc.getLocation()) && !npc.isInCombat();
				}
			});
			if (shade == null && room.getNpc(SHADES) == null){
				if (Calculations.distanceTo(getMonolith()) > 4 && !Players.getLocal().isMoving()){
					getMonolith().getLocation().clickOnMap();
					Task.sleep(800);
				}
				return 1;
			}
			if (shade != null){
				if (Calculations.distanceTo(shade) > 5){
					shade.getLocation().clickOnMap();
				}else{
					shade.interact("Attack");
					for (int i = 0; i < 100 && !shade.isInCombat(); i++) Task.sleep(35);
				}
			}
			return 1;
		}
		if (Calculations.distanceTo(getMonolith()) > 4){
			getMonolith().getLocation().clickOnMap();
		}else{
			getMonolith().interact("Activate");
		}
		Task.sleep(2000);
		return 1;
	}

	@Override
	public boolean isFinished() {
		for (Room solvedRoom : solvedRooms){
			if (solvedRoom.getLocation().equals(room.getLocation())) return true; 
		}
		if (solved) solvedRooms.add(room);
		return solved;
	}

	@Override
	public void messageReceived(MessageEvent e) {
		if (e.getMessage().contains("full charge")) solved = true;
	}

	@Override
	public int[] itemNeed() {
		return new int[0];
	}

	@Override
	public void onRepaint(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}
	
	
}
